/************************************************************
Copyright (C) Xiong,Hui at www.51embed.com
FileName: init.c
Author: Xiong,Hui huixiong73@gmail.com
Date:08/3/13
Description: uart process
Version: 1.0
Function List: 
    1. uart_rece();
History: 
    1. Huixiong 08/4/19 build this moudle
***********************************************************/
#include <intrins.h>
#include <string.h>
#include "at89x52.H"
#include "uart.h"
#include "GUI.h"
#include "GUI_Key.h"
#include "Pub.h"
/*
typedef struct{
  OS_INT8S *pName;
  OS_INT8U (*pCmdFunc)(OS_INT8U Argc,OS_INT8S **Argv);
}S_OS_Shell_Cmd;
*/
extern INT8U Shell_Key(INT8U Argc, INT8S **Argv);

const S_OS_Shell_Cmd Shell_Cmd[]=
{
  {"k", Shell_Key}
};
/*
#define GUI_KEY_BACKSPACE         8         
#define GUI_KEY_TAB               9         
#define GUI_KEY_BACKTAB           10
#define GUI_KEY_ENTER             13        
#define GUI_KEY_LEFT              16
#define GUI_KEY_UP                17
#define GUI_KEY_RIGHT             18
#define GUI_KEY_DOWN              19
#define GUI_KEY_HOME              23
#define GUI_KEY_END               24
#define GUI_KEY_SHIFT             25
#define GUI_KEY_CONTROL           26
#define GUI_KEY_ESCAPE            27        
#define GUI_KEY_INSERT            29
#define GUI_KEY_DELETE            30
#define GUI_KEY_SPACE             32
#define GUI_KEY_PGUP              33
#define GUI_KEY_PGDOWN            34
*/

INT8U Shell_Key(INT8U argc, INT8S **argv)
{
  //TRACE();
  
  if(*(argv[0]) EQ 'l')
  {
    GUI_Key = GUI_KEY_LEFT;
  }
  else if(*(argv[0]) EQ 'r')
  {
    GUI_Key = GUI_KEY_RIGHT;
  }
  else if(*(argv[0]) EQ 'u')
  {
    GUI_Key = GUI_KEY_UP;
  }
  else if(*(argv[0]) EQ 'd')
  {
    GUI_Key = GUI_KEY_DOWN;
  }
  else if(*(argv[0]) EQ 'e')
  {
    GUI_Key = GUI_KEY_ENTER;
  }  
  else if(*(argv[0]) EQ 't')
  {
    GUI_Key = GUI_KEY_TAB;
  }
  else
    GUI_Key = *(argv[0]);

  return 1;
}

/*************************************************************
*  Function:uart_rece()
*  Description:	a simple example that show the data received by uart being proessed 
*  Calls:
*  Called by:main()
*  Input:
*  Output:
*  Return:
*  Others:
*  History:
       1. Huixiong 08/4/19 created
*************************************************************/
extern void clear_uart_recebuf();
void uart_rece(void)
{
  if(OS_Cmd_Analys(Shell_Cmd, S_NUM(Shell_Cmd), uartrecebuf, uartreceptr) > 0)
  {
    clear_uart_recebuf();
  }
/*	
	if(uartreceflag == TRUE){    //uart has received a data frame ended by \r\n
	   uartreceflag = FALSE; 
	   while(echoflag || uartsendflag ){  //wait until there is no data waiting for sending
	       ;
	   }

	   if(uartreceptr >=3){
			 strcpy(&uartsendbuf,&wrong);
			 uartsendptr=sizeof(wrong);
	   }else{
	       switch(uartrecebuf[0]){
		       case '0':
			       strcpy(&uartsendbuf,&right0);
				   uartsendptr=sizeof(right0);
				   break;
			   case '1':
			       strcpy(&uartsendbuf,&right1);
				   uartsendptr=sizeof(right1);
				   break;
			   case '2':
			       strcpy(&uartsendbuf,&right2);
				   uartsendptr=sizeof(right2);
				   break;
			   case '3':
			       strcpy(&uartsendbuf,&right3);
				   uartsendptr=sizeof(right3);
				   break;
			   case '4':
			       strcpy(&uartsendbuf,&right4);
				   uartsendptr=sizeof(right4);
				   break;
			   case '5':
			       strcpy(&uartsendbuf,&right5);
				   uartsendptr=sizeof(right5);
				   break;
			   case '6':
			       strcpy(&uartsendbuf,&right6);
				   uartsendptr=sizeof(right6);
				   break;
			   case '7':
			       strcpy(&uartsendbuf,&right7);
				   uartsendptr=sizeof(right7);
				   break;
			   default:
			   	    strcpy(&uartsendbuf,&right);
				   uartsendptr=sizeof(right);
			       break;
		   }
	   }
	   uartreceptr = 0x0;
	   uartsendtempptr = 0x0;
	   uartsendflag = TRUE;
	   TI = 1;						//intiates the sending
	}
	*/
}

void clear_uart_recebuf()
{
  uartreceptr = 0;
  memset(uartrecebuf, 0, sizeof(uartrecebuf));
}

void OS_Put_Char(char c)
{
  SBUF = c;
  while(!TI);
  TI = 0;
}
